<?php

namespace app\api\controller;

use app\common\model\Help;
use think\Controller;
use think\Db;
use think\Request;
use think\Response;
use app\api\service\DictService;
use app\api\validate\Dict as DictValidate;

class Dict extends Controller
{
    protected $dictService;
    protected $dictValidate;

    protected function initialize()
    {
        parent::initialize();
        $this->dictService = new DictService();
        $this->dictValidate = new DictValidate();
    }

    /**
     * 统一响应格式
     * @param int $code 状态码
     * @param string $message 消息
     * @param mixed $data 数据
     * @return Response
     */
    protected function json($code = 200, $message = 'success', $data = null)
    {
        $result = [
            'code' => $code,
            'message' => $message,
            'data' => $data
        ];

        return json($result);
    }

    /**
     * 字典类型列表
     * @return Response
     */
    public function dictTypeList()
    {
        try {
            $params = $this->request->param();
            $result = $this->dictService->getDictTypeList($params['params']);
            return $this->json(200, 'success', $result);
        } catch (\Exception $e) {
            return $this->json(500, $e->getMessage());
        }
    }

    /**
     * 字典类型详情
     * @param int $dictId
     * @return Response
     */
    public function dictTypeDetail($dictId)
    {
        try {
            if (!$dictId) {
                return $this->json(400, '字典ID不能为空');
            }

            $result = $this->dictService->getDictTypeDetail($dictId);
            if (!$result) {
                return $this->json(404, '字典类型不存在');
            }

            return $this->json(200, 'success', $result);
        } catch (\Exception $e) {
            return $this->json(500, $e->getMessage());
        }
    }

    /**
     * 新增字典类型
     * @return Response
     */
    public function addDictType()
    {
        try {
            $data = $this->request->post();
            // 数据验证
            if (!$this->dictValidate->scene('typeAdd')->check($data)) {
                return $this->json(400, $this->dictValidate->getError());
            }

            $result = $this->dictService->addDictType($data);

            $help = new Help();
            $help->system_log($data, '新增字典类型');

            return $this->json(200, '新增成功', $result);
        } catch (\Exception $e) {
            return $this->json(500, $e->getMessage());
        }
    }

    /**
     * 修改字典类型
     * @return Response
     */
    public function updateDictType()
    {
        try {
            $data = $this->request->put();

            // 数据验证
            if (!$this->dictValidate->scene('typeEdit')->check($data)) {
                return $this->json(400, $this->dictValidate->getError());
            }

            if (!isset($data['dict_id'])) {
                return $this->json(400, '字典ID不能为空');
            }

            $result = $this->dictService->updateDictType($data);

            $help = new Help();
            $help->system_log($data, '修改字典类型');

            return $this->json(200, '修改成功', $result);
        } catch (\Exception $e) {
            return $this->json(500, $e->getMessage());
        }
    }

    /**
     * 删除字典类型
     * @param string $dictIds
     * @return Response
     */
    public function deleteDictType()
    {
        $dictIds = input('dictIds') ?? '';

        try {
            if (!$dictIds) {
                return $this->json(400, '请选择要删除的字典类型');
            }

            $dictIds = explode(',', $dictIds);
            $result = $this->dictService->deleteDictType($dictIds);

            $help = new Help();
            $help->system_log($dictIds, '删除字典类型');

            return $this->json(200, '删除成功', $result);
        } catch (\Exception $e) {
            return $this->json(500, $e->getMessage());
        }
    }

    /**
     * 字典数据列表
     * @return Response
     */
    public function dictDataList()
    {
        try {
            $params = $this->request->param();
            $result = $this->dictService->getDictDataList($params['params']);
            return $this->json(200, 'success', $result);
        } catch (\Exception $e) {
            return $this->json(500, $e->getMessage());
        }
    }

    /**
     * 根据字典类型获取字典数据
     * @param string $dictType
     * @return Response
     */
    public function dictDataByType($dictType)
    {
        try {
            if (!$dictType) {
                return $this->json(400, '字典类型不能为空');
            }

            $result = $this->dictService->getDictDataByType($dictType);
            return $this->json(200, 'success', $result);
        } catch (\Exception $e) {
            return $this->json(500, $e->getMessage());
        }
    }

    /**
     * 字典数据详情
     * @param int $dictCode
     * @return Response
     */
    public function dictDataDetail($dictCode)
    {
        try {
            if (!$dictCode) {
                return $this->json(400, '字典编码不能为空');
            }

            $result = $this->dictService->getDictDataDetail($dictCode);
            if (!$result) {
                return $this->json(404, '字典数据不存在');
            }

            return $this->json(200, 'success', $result);
        } catch (\Exception $e) {
            return $this->json(500, $e->getMessage());
        }
    }

    /**
     * 新增字典数据
     * @return Response
     */
    public function addDictData()
    {
        try {
            $data = $this->request->post();

            // 数据验证
            if (!$this->dictValidate->scene('dataAdd')->check($data)) {
                return $this->json(400, $this->dictValidate->getError());
            }

            $result = $this->dictService->addDictData($data);

            $help = new Help();
            $help->system_log($data, '新增字典数据');

            return $this->json(200, '新增成功', $result);
        } catch (\Exception $e) {
            return $this->json(500, $e->getMessage());
        }
    }

    /**
     * 修改字典数据
     * @return Response
     */
    public function updateDictData()
    {
        try {
            $data = $this->request->put();

            // 数据验证
            if (!$this->dictValidate->scene('dataEdit')->check($data)) {
                return $this->json(400, $this->dictValidate->getError());
            }

            if (!isset($data['dict_code'])) {
                return $this->json(400, '字典编码不能为空');
            }

            $result = $this->dictService->updateDictData($data);

            $help = new Help();
            $help->system_log($data, '修改字典数据');

            return $this->json(200, '修改成功', $result);
        } catch (\Exception $e) {
            return $this->json(500, $e->getMessage());
        }
    }

    /**
     * 删除字典数据
     * @param string $dictCodes
     * @return Response
     */
    public function deleteDictData()
    {
        $dictCodes = input('dictCodes') ?? '';
        try {
            if (!$dictCodes) {
                return $this->json(400, '请选择要删除的字典数据');
            }

            $dictCodes = explode(',', $dictCodes);
            $result = $this->dictService->deleteDictData($dictCodes);

            $help = new Help();
            $help->system_log($dictCodes, '删除字典数据');

            return $this->json(200, '删除成功', $result);
        } catch (\Exception $e) {
            return $this->json(500, $e->getMessage());
        }
    }

    /**
     * 刷新字典缓存
     * @return Response
     */
    public function refreshDictCache()
    {
        try {
            $result = $this->dictService->refreshDictCache();
            return $this->json(200, '刷新缓存成功', $result);
        } catch (\Exception $e) {
            return $this->json(500, $e->getMessage());
        }
    }

    /**
     * 获取字典类型选项
     * @return Response
     */
    public function dictTypeOptions()
    {
        try {
            $result = $this->dictService->getDictTypeOptions();
            return $this->json(200, 'success', $result);
        } catch (\Exception $e) {
            return $this->json(500, $e->getMessage());
        }
    }
}